import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.*;

public class MainPanel extends JPanel implements MouseListener
{
	public final static int RAZA=20;
	
	private int from=-1;
	private ArrayList<Point> coordonate;
	private CheckBox check = new CheckBox("Graf Neorientat"); 
	private GrafOrientat graf;
	
	public MainPanel(GrafOrientat graf)
	{
		this.graf=graf;
		addMouseListener(this);
		coordonate=new ArrayList<Point>();
		setLayout(new FlowLayout());
		add(check.getCheckBox());
		SouthPanel sp = new SouthPanel(graf);
	}
	
	private int citestevaloare()
	{
		String number;
		number = JOptionPane.showInputDialog("Citeste arcul");
		System.out.println(number);
		return Integer.parseInt(number);
	}
	
	public int getVarf(int x, int y)
	{
		for(int i=0;i<coordonate.size();i++)
		{
			Point p=coordonate.get(i);
			if(distance(x, y, p.getX(), p.getY())<=2*RAZA)
				return i;
		}
		return -1;
	}
	
	private static double distance(int x1, int y1, int x2, int y2)
	{
		return Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
	}
	
	public Point getCoord(int varf)
	{
		return coordonate.get(varf);
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		
		for(int i=0;i<graf.getNrVarfuri();i++)
		{
			Point p=getCoord(i);
			g.drawOval(p.getX()-RAZA, p.getY()-RAZA, 2*RAZA, 2*RAZA);
			g.drawString(i+"", p.getX()-3, p.getY()+5);
		}
		
		if(!graf.getListaAd().isEmpty())
		{
		for(int i=0;i<graf.getNrVarfuri();i++)
		{
			
			Point from = getCoord(i);
			ArrayList<Integer> vecini = graf.getListaAd(i);
			for (int j : vecini)
			{
				Point to = getCoord(j);
				int x1=from.getX(), y1=from.getY();
				int x2=to.getX(), y2=to.getY();
				
				double k=RAZA/(distance(x1, y1, x2, y2)-RAZA);
				int x1f=(int) ((x1+k*x2)/(k+1));
				int y1f=(int) ((y1+k*y2)/(k+1));
				int x2f=(int) ((x1*k+x2)/(k+1));
				int y2f=(int) ((y1*k+y2)/(k+1));
				
				int cost = graf.cautaInListInc(i,j);
				g.drawString(cost+"", (x1+x2)/2+10, (y1+y2)/2+25);
				//g.drawLine(from.getX(), from.getY(), to.getX(), to.getY());
				sageata(x1f, y1f, x2f, y2f, g);
			}	
		}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e)
	{
		// TODO Auto-generated method stub
		//JOptionPane.showMessageDialog(null, "Mouse pressed at "+e.getX()+","+e.getY()+" butonul: "+e.getButton());
		
		check.setEnableFalse();
		
		int x=getVarf(e.getX(), e.getY());
		if(x==-1)
		{
			int vf=graf.addVarf();
			coordonate.add(new Point(e.getX(), e.getY()));
			repaint();
		}
		else
		{
			if(from==-1)
				from=x;
			else
			{
				int y=getVarf(e.getX(), e.getY());
				if(y==-1)
					from=-1;
				else
				{
					graf.addArc(from, y);
					int z = citestevaloare();
					graf.addListaInc(from,y, z);
					if(check.getCheckValue())
						{
							graf.addArc(y, from);
							graf.addListaInc(y,from, z);
						}
					repaint();
					from=-1;
				}
			}
		}
	}

	@Override
	public void mouseReleased(MouseEvent e)
	{
		// TODO Auto-generated method stub
	}
	
	private void sageata(int x1, int y1, int x2, int y2, Graphics g)
	{
		double ANGLE=Math.PI/8;
		int x=x2-x1;
		int y=y2-y1;
		double lungime=Math.sqrt(x*x+y*y);
		double lungimeSageata=10;	
		double unghi=Math.acos(x/lungime);
		
		if(y<0)
		{
		double angle=Math.PI-ANGLE-unghi;
		g.drawLine(x1+x, y1+y,
				x1+(int)(x+lungimeSageata*Math.cos(angle)),
				y1+(int)(y+lungimeSageata*Math.sin(angle)));	
		angle+=2*ANGLE;	
		g.drawLine(x1+x,y1+y,
				x1+(int)(x+lungimeSageata*Math.cos(angle)),
				y1+(int)(y+lungimeSageata*Math.sin( angle)));	
		}
		else
		{
		double angle=Math.PI-ANGLE+unghi;
		g.drawLine(x1+x,y1+y,
				x1+(int)(x+lungimeSageata*Math.cos(angle)),
				y1+(int)(y+lungimeSageata*Math.sin( angle)));	
		angle+=2*ANGLE;	
		g.drawLine(x1+x,y1+y,
				x1+(int)(x+lungimeSageata*Math.cos(angle)),
				y1+(int)(y+lungimeSageata*Math.sin(angle)));	

		}

		
		g.drawLine(x1, y1, x2, y2);
		
	}
}
